<extend name="Base/common" />

<block name="style">
	<link rel="stylesheet" type="text/css" href="__PUBLIC__/Api/css/order.css">
</block>

<block name="Content">

<div class="page-group">
    <div class="page page-current">
        <div class="content">
            <form action="" class="m-phone">
                <ul>
                    <li>
                    	<i class="icon-phone-num">
                    	</i><input type="text" name="tel" id="tel" placeholder="请输入新手机号">
                    </li>
                    <li>
                        <i class="icon-valid"></i><input type="text" name="validation" id="validation" placeholder="请输入验证码"><a class="get-valid" href="#" id="validate">获取验证码</a>
                    </li>
                </ul>
                <div class="confirm">
                    <a class="button button-fill button-big" type="button" onclick="update()">确定</a>
                </div>
            </form>
        </div>
    </div>
</div>

</block>

<block name="script">
<script type="text/javascript">
$("#validate").click(sendCode);

function sendCode(){
	
	var tel = $.trim($("#tel").val()) || "";
	var type = "3";
	var target = Think.U('Public/validate');
	
	if(!/^1(3|4|5|7|8)\d{9}$/.test(tel))){ 
        $.alert("手机号码格式有误，请确认");  
        return false; 
    }
	
	$.post(target, {'tel':tel, 'type':type}, function(data){
		
		if(data.status == '200'){
			$.alert('发送成功');
			//倒计时120秒
			setTime = 120;
			clearCount = setInterval(countDown,1000);
		}else if(data.status == '108'){
			$.alert('账号已被注册');
		}else if(data.status == '109'){
			$.alert('账号不存在');
		}else if(data.status == '200'){
			$.alert('发送成功');
		}else{
			$.alert('发送失败，请刷新后重试');
		}
	});
}


function countDown(){
	$("#validate").unbind("click");
	$("#validate").html(setTime+"秒后重发");
	setTime--;
	if(setTime <= 0){
		clearInterval(clearCount);
		$("#validate").click(sendCode);
		$("#validate").html("获取验证码");
	}
}

function update(){
	
	var tel = $.trim($("#tel").val()) || "";
	var validation = $.trim($("#validation").val()) || "";
	var target = Think.U('User/updateAccount');
	
	if(!(/^1((3|4|5|8){1}\d{1}|70)\d{8}$/.test(tel))){ 
        $.toast("手机号码格式有误，请确认");  
        return false; 
    }else if(isNaN(validation)){
    	$.toast("验证码错误");  
        return false; 
    }
	
	$.post(target, {'tel':tel, 'validation':validation}, function(data){
		
		if(dataObj.status == '422'){
			$.alert("参数异常");
		}else if(dataObj.status == '103'){
			$.alert('验证码错误');
		}else if(dataObj.status == '108'){
			$.alert('账号已被注册');
		}else if(dataObj.status == '301'){
			$.alert('操作失败');
		}else if(dataObj.status == '200'){
			$.alert('操作成功', function(){
				window.location.href = Think.U('Public/logout');
			});
		}
	});
}
</script>
</block>

